package com.xixi.client;

import com.xixi.model.User;
import com.xixi.model.userMessage;
import com.xixi.model01.Student;
import com.xixi.model01.StudentMessage;
import jdk.nashorn.internal.ir.BaseNode;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

//编程实现客户端的主界面绘制和相应功能的实现
public class clientView {
    //为了可以使用输入输出流对象，准备采用合成复用原则来实现
    private ClientInitClose clientInitClose;

    //通过构造方法实现成员变量的初始化
    public clientView(ClientInitClose clientInitClose) {
        this.clientInitClose = clientInitClose;
    }

    //自定义成员方法来绘制主界面
    public void clientMap() throws IOException, ClassNotFoundException {
        while (true) {
            System.out.println("\t========在线考试系统=========");
            System.out.println("     =====欢迎进入=============");
            System.out.println("=========1  学员系统===========");
            System.out.println("=========2  管理系统===========");
            System.out.println("=========0  退出系统===========");
            System.out.println("=======请自己选择编号进行：======");
     /*   Scanner sc = new Scanner(System.in);
        int choose = sc.nextInt();*/
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose) {
                case 1:
                    clientstudentLogin();
                    System.out.println("正在进行学员考试系统");
                    break;
                case 2:
                    clientManagerLogin();//这里也是要抛出异常
                    System.out.println("正在进入管理系统");
                    break;
                case 0:
                    System.out.println("退出该系统");
                    System.exit(0);
                    return;
                default:
                    System.out.println("输入错误，请重新");
            }
        }
    }

    private void clientstudentLogin() throws IOException, ClassNotFoundException {
        //  准备学员用户登录的相关数据
        System.out.println(" 请输入学员的账户信息：");
        String userName = ClientScanner.getScanner().next();
        System.out.println("请输入学员的密码信息：");
        String password = ClientScanner.getScanner().next();
        userMessage tem = new userMessage("managerCheck", new User(userName, password));
        //2.将userMassage类型的对象通过对象输出流发送给服务器
        clientInitClose.getObjectOutputStream().writeObject(tem);//又进行抛出异常
        System.out.println("客户端发送学员账户信息成功");
        tem = (userMessage) clientInitClose.getObjectInputStream().readObject();
        if("success".equals(tem.getType())){
            while (true){
                System.out.println("考试模块\t  1.开始考试\t  2.导出成绩\t  3.查询成绩\t");
                String stuSev=ClientScanner.getScanner().next();
                if(stuSev.equals("1")){
                    System.out.println("开始考试   考试中");
                }
                else if(stuSev.equals("2")){
                    System.out.println("导出成绩");
                }
                else if(stuSev.equals("3")){
                    System.out.println("查询成绩");
                }
                else {
                    System.out.println("输入错误");
                    break;
                }
            }
        }
    }

    //自定义成员方法实现客户端管理员登录的功能
    private void clientManagerLogin() throws IOException, ClassNotFoundException {
        //1.准备管理员登录的相关数据
        System.out.println("请输入管理员的账户信息：");
        String userName = ClientScanner.getScanner().next();
        System.out.println("请输入管理员的密码信息：");
        String password = ClientScanner.getScanner().next();
        userMessage tem = new userMessage("managerCheck", new User(userName, password));
        //2.将userMassage类型的对象通过对象输出流发送给服务器
        clientInitClose.getObjectOutputStream().writeObject(tem);//又进行抛出异常
        System.out.println("客户端发送管理员账户信息成功");
        //接收服务器的处理结果并给出提示
        tem = (userMessage) clientInitClose.getObjectInputStream().readObject();
        if ("success".equals(tem.getType())) {
            while (true) {
                System.out.println("登录成功，欢迎使用\t 1:学员管理模块\t 2:考题管理模块 \t 0:退出");
                //String Msl=ClientScanner.getScanner().nextLine();
                String Msl = ClientScanner.getScanner().next();
                if (Msl.equals("1")) {
                    System.out.println("学员管理模块");
                   // tem = new userMessage("StudentManager", null);//这里是UserMessage
                    tem = new userMessage("studentManager", null);//这里是UserMessage
                    //发送给服务器
                    clientInitClose.getObjectOutputStream().writeObject(tem);
                    while (true) {
                        System.out.println("欢迎来到学员管理模块\t 1: 增加学员信息\t 2：删除学员\t 3：修改学员\t 4: 查询学员\t 5：遍历学员 \t 0: 退出");
                        //String stuSel=ClientScanner.getScanner().nextLine();
                        String stuSel = ClientScanner.getScanner().next();
                        if (stuSel.equals("1")) {
                            System.out.println("增加学员开始");
                            System.out.println("请输入学员的账号信息");
                            String stuName = ClientScanner.getScanner().next();
                            System.out.println("请输入学员密码信息");
                            String stuPassword = ClientScanner.getScanner().next();
                            StudentMessage studentMessage = new StudentMessage("addStudent", new Student(stuName, stuPassword));
                            //接收服务器的处理结果并给出提示
                            clientInitClose.getObjectOutputStream().writeObject(studentMessage);
                            System.out.println("客户端发送管理员账户信息成功！");
                            //这里需要强制性转换 也是后面报错得原因
                            studentMessage=(StudentMessage) clientInitClose.getObjectInputStream().readObject();
                            if (studentMessage.getType().equals("success")) {
                                System.out.println("增加学员成功");
                            } else {
                                System.out.println("增加学员失败，该用户已存在");
                            }
                        } else if (stuSel.equals("2")) {
                            System.out.println("删除学员成功");
                        } else if (stuSel.equals("3")) {
                            System.out.println("修改学员成功");
                        } else if (stuSel.equals("4")) {
                            System.out.println("查询学员成功");
                        } else if (stuSel.equals("5")) {
                            System.out.println("遍历学员成功");
                            StudentMessage studentMessage = new StudentMessage("getAllstudent", null);
                            //接收服务器的处理结果并给出提示
                            clientInitClose.getObjectOutputStream().writeObject(studentMessage);
                            List<Student> studentList = (List<Student>) clientInitClose.getObjectInputStream().readObject();//对象输入流来读取
                            //遍历学员信息
                            for (int i = 0; i < studentList.size(); i++) {
                                System.out.println(studentList.get(i));
                            }
                        } else if (stuSel.equals("0")) {
                            StudentMessage studentMessage = new StudentMessage("stuexit", null);
                            clientInitClose.getObjectOutputStream().writeObject(studentMessage);
                            break;
                        } else {
                            System.out.println("输入有误！");
                        }
                    }
                } else if (Msl.equals("2")) {
                    System.out.println("考题管理模块");
                    tem = new userMessage("testPaperManager", null);
                    clientInitClose.getObjectOutputStream().writeObject(tem);
            while (true){
                System.out.println("欢迎来到考题管理模块，1：增加考题   2：修改考题  3：删除考题   4：查找考题  5：导入考题   0:退出");
                String stuSeve=ClientScanner.getScanner().next();
                if (stuSeve.equals("1")){
                    System.out.println("增加考题中。。。。。");
                }else if (stuSeve.equals("2")){
                    System.out.println("修改考题中。。。。");
                }else if (stuSeve.equals("3")){
                    System.out.println("删除考题。。。。");
                }else if (stuSeve.equals("3")) {
                    System.out.println("查询考题");
                }else if (stuSeve.equals("4")){
                    System.out.println("导入考题");
                }else if (stuSeve.equals("0")){
                    System.out.println("退出");
                    break;
                }else {
                    System.out.println("输入有误");
                }
            }
                } else if (Msl.equals("0")) {
                    tem = new userMessage("exit", null);
                    clientInitClose.getObjectOutputStream().writeObject(tem);
                    break;
                } else {
                    System.out.println("输入有误");
                }
            }
        } else {
            System.out.println("用户名和密码错误，请重新输入");
        }

    }


    public void clientMainPage() {
    }
}






